package leecode

import "strings"

func longestPalindrome(s string) string {
	if len(s) == 1 {
		return s
	}
	if len(s) == 2 && s[0] != s[1] {
		return s[0:1]
	}
	//首先反转字符串 babad dabab
	//将字符串转化为一个rune切片
	sRunes := []rune(s)
	length := len(sRunes)
	//使用两个索引交换字符位置
	for i, j := 0, length-1; i < j; i, j = i+1, j-1 {
		//交换
		sRunes[i], sRunes[j] = sRunes[j], sRunes[i]
	}
	reverseS := string(sRunes)
	//拿到两个字符串的公共部分
	var commonSubstring string
	for i := 0; i < len(reverseS); i++ {
		for j := i + 1; j <= len(reverseS); j++ {
			substring := s[i:j]
			if len(substring) > len(commonSubstring) && strings.Contains(reverseS, substring) {
				commonSubstring = substring
			}
		}
	}
	return commonSubstring
}
